from flask_sqlalchemy import SQLAlchemy
from Flask_webblog.extensions import bcrypt

# INIT the sqlalchemy object
# Will be load the SQLALCHEMY_DATABASE-URL from confi.py
# SQLAlchemy 会自动的从app对象的DevConfig中加载连接数据库的配置项
db = SQLAlchemy()

class User(db.Model):
    """Represents Proected users"""
    #Set the name for table
    __tablename__ = 'users'
    id = db.Column(db.String(45),primary_key=True)
    username = db.Column(db.String(255))
    password = db.Column(db.String(255))
    #Establish contact with Post's ForeighKey:user_id
    posts = db.relationship(
        'Post',
        backref='users',
        lazy='dynamic'
    )


    def __init__(self,id,username,password):
        self.id = id
        self.username = username
        self.password = self.set_password(password)

    def __repr__(self):
        '''Define the string format for instance of User.'''
        return '<Model User `{}`>'.format(self.username)
    
    def set_password(self,password):
        return bcrypt.generate_password_hash(password)
    def check_password(self,password):
        return bcrypt.check_password_hash(self.password,password)

posts_tags = db.Table('posts_tags',
    db.Column('post_id',db.String(45),db.ForeignKey('posts.id')),
    db.Column('tag_id', db.String(45), db.ForeignKey('tags.id')))

class Post(db.Model):
    '''Represents Proected posts'''
    __tablename__ = 'posts'
    id = db.Column(db.String(45),primary_key=True)
    title = db.Column(db.String(255))
    text = db.Column(db.Text())
    publish_date = db.Column(db.DateTime())
    #Set the foreign key for Post 
    user_id = db.Column(db.String(45),db.ForeignKey('users.id'))
    comments = db.relationship(
        'Comment',
        backref='posts',
        lazy='dynamic'
    )
    tags = db.relationship(
        'Tag',
        secondary=posts_tags,
        backref=db.backref('posts',lazy='dynamic')
    )

    def __init__(self,title,id):
        self.id = id
        self.title = title
    
    def __repr__(self):
        return "<Model Post `{}`>".format(self.title)

class Comment(db.Model):
    """Represents Proected comments."""
    __tablename__ = 'comments'
    id = db.Column(db.String(45),primary_key=True)
    name = db.Column(db.String(255))
    text = db.Column(db.Text())
    date = db.Column(db.DateTime())
    post_id = db.Column(db.String(45),db.ForeignKey('posts.id'))

    def __ini__(self,id,name):
        self.id = id
        self.name = name
    def __repr__(self):
        return '<Model Comment `{}`'.format(self.name)

class Tag(db.Model):
    '''Represents Proected tags.'''
    __tablename__ = 'tags'
    id = db.Column(db.String(45),primary_key=True)
    name = db.Column(db.String(255))
    def __init__(self,id,name):
        self.name = name
        self.id = id
    def __repr__(self):
        return "<Model Tag `{}`>".format(self.name)